# 导入蓝图包
from flask import Blueprint,request,jsonify
import json

from data import get_data

# 创建蓝图
bp_course = Blueprint('course',__name__)
from db import db1

# 接口
@bp_course.route('/couponM',methods=['POST','GET','PUT','DELETE'])
def couponM():
    # 获取数据
    if request.method == 'POST':
        data = request.data
        if data:
            data = json.loads(data)
        else:
            data = request.form
        # 构造SQL语句
        sql = """insert into coupon values(0,'%s','%d','%s','%s')"""%(data['name'],int(data['total']),data['starttime'],data['endtime'])
        # db添加
        db1.intsert(sql)
        # 返回结果
        return '添加成功'
    elif request.method == 'GET':
        # 根据id获取出数据
        id = request.args.get('id')
        sql = """select * from coupon where id=%d"""%(int(id))
        res = db1.find(sql)
        return jsonify({'code':200,'coupon':res})

    elif request.method == 'PUT':
        # 获取用户输入的数据
        data = request.data
        if  data:
            data = json.loads(data)
        else:
            data = request.form
        print(data)
        # 构造sql语句  update 表名 set 字段名1=值,字段名2=值 where id=1
        sql = """update coupon set name='%s',total=%d,starttime='%s',endtime='%s' where id=%d"""%(data['name'],int(data['total']),data['starttime'],data['endtime'],int(data['id']))
        print(sql)
        db1.intsert(sql)
        return jsonify({'code':200})

    elif request.method == 'DELETE':
        # 获取id
        id = request.args.get('id')
        print(id)
        # 删除获取到的id的数据
        sql = "delete from coupon where id=%d"%(int(id))
        print(sql)
        # 删除方法
        db1.intsert(sql)
        return jsonify({'code':200})

# section部门表
@bp_course.route('/section',methods=['POST','GET','PUT','DELETE'])
def section():
    if request.method == 'POST':
        # 获取传过来的数据
        data = request.data
        if data:
            data = json.loads(data)
        else:
            data = request.form
        print(data)
        # 构造sql 语句
        sql = """insert into section values(0,"%s",%d)"""%(data['name'],int(data['sum']))
        print(sql)
        # db添加
        db1.intsert(sql)
        # 返回结果
        return jsonify({'code':200})
    elif request.method == 'GET':
        data = request.form
        sql = """select * from section"""
        res = db1.fingall(sql)
        return jsonify({'code':200,'sections':res})

    elif request.method == 'PUT':
        pass
    elif request.method == 'DELETE':
        pass

# staff员工表
@bp_course.route('/staff',methods=['POST','GET','PUT','DELETE'])
def staff():
    if request.method == 'POST':
        # 获取数据
        data = get_data()
        # 构造sql语句
        #   select * from 表1 left join 表2 on 表1的id = 表2的外键  where id = data['pid']
        sql1 = '''select * from section where id = %d '''%(int(data['pid']))
        res = db1.find(sql1)
        print(res)
        sum = int(res['sum'])+1
        sql2 = 'update section set sum = %d where id = %d '%(sum,int(data['pid']))
        db1.intsert(sql2)
        sql = """insert into staff values(0,"%s","%s","%d","%d")"""%(data['name'],data['gender'],int(data['salary']),int(data['pid']))
        # db添加
        db1.intsert(sql)
        # 返回结果
        return jsonify({'code':200})

    # 获取出所有的数据
    elif request.method == 'GET':
        data = request.form
        sql = """select * from staff"""
        res = db1.fingall(sql)
        return jsonify({'code':200,'sections':res})

    # elif request.method == 'PUT':
        # 获取出来数据
        # id = request.args.get('id')

    # 删除数据
    elif request.method == 'DELETE':
        id = request.args.get('id')
        sql = """delete from staff where id=%d"""%(int(id))
        db1.intsert(sql)
        return jsonify({'code':200})
